﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.Cryptography;
using ScheduleMaker.ConfigManager;
using ScheduleMaker.DB.CoreDAO;
using ScheduleMaker.DB;
using ScheduleMaker.DB.Domain.Core;
using ScheduleMaker.DB.Domain;
using System.Text.RegularExpressions;


namespace ScheduleMaker
{
    public partial class smAdmUserAddForm : Form
    {
        smAdminForm pointer;

        /*
        public smAdmUserAddForm()
        {
            InitializeComponent();
        }
        */
 
        public smAdmUserAddForm( smAdminForm pointer)
        {
            InitializeComponent();
            this.pointer = pointer;
        }

        public string EncodePassword(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            String md5hash = BitConverter.ToString(encodedBytes);

            return md5hash.Replace("-", "").ToLower();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (regexpHelper.Instance.isNameCorrect(tbusername.Text) && regexpHelper.Instance.isNameCorrect(tbusersurname.Text) && regexpHelper.Instance.isNickCorrect(tblogin.Text))
            {


                // Dodawanie nowego użytownika - w trybie dla SZKOLY
                //DB.DAO dao = new DB.DAO();

                UserDAO userDao = DAOFactory.getUserDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                TeacherDAO teacherDao = DAOFactory.getTeacherDAO(ConfigManager.ConfigManager.getConfiguration().Type);
                LevelDAO levelDao = DAOFactory.getLevelDAO(ConfigManager.ConfigManager.getConfiguration().Type);

                User user;
                Teacher teacher;
                Config config = ConfigManager.ConfigManager.getConfiguration();
                Config configForNewUser = new Config();
                configForNewUser.Addr = config.Addr;
                configForNewUser.Login = config.Login;
                configForNewUser.Password = config.Password;
                configForNewUser.Type = config.Type;
                if (config.Type == Mode.School)
                {
                    teacher = new TeacherSchool();
                    user = new UserSchool();
                }
                else
                {
                    teacher = new TeacherUniversity();
                    user = new UserUniversity();
                }

                if (cbuserlevel.Text == "Prowadzący")
                {
                    teacher.Level = levelDao.getByName("TEACHER");
                    teacher.Firstname = tbusername.Text;
                    teacher.Surname = tbusername.Text;
                    teacher.Login = tblogin.Text;
                    teacher.Password = EncodePassword(tbpass.Text);
                    teacher.Name = tblogin.Text;
                    teacherDao.save(teacher);
                }
                else
                {
                    //if (cbuserlevel.Text == "Pracownik")
                    //user.Level = levelDao.getByName("ADMIN");
                    //else
                    user.Level = levelDao.getByName("NORMAL_USER");

                    user.Firstname = tbusername.Text;
                    user.Surname = tbusersurname.Text;
                    user.Login = tblogin.Text;
                    user.Password = EncodePassword(tbpass.Text);
                    userDao.save(user);
                }
                // Tworzenie pliku dla użytkownika

                SaveFileDialog saveFileDialog = new SaveFileDialog();
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    String path = saveFileDialog.FileName;
                    ConfigManager.ConfigManager.saveConfiguration(path, tbpass.Text, configForNewUser);
                }
                try
                {

                }
                catch (Exception ex)
                {

                }

                MessageBox.Show("Użytkownik został dodany", "Dodanie użytkownika", MessageBoxButtons.OK, MessageBoxIcon.Information);

                pointer.getUserList();
                pointer.getTeacherList();

                this.Dispose();
            }
            else
            {
                MessageBox.Show("Niepoprawne dane!", "Dodanie użytkownika", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void smAdmUserAddForm_Load(object sender, EventArgs e)
        {

        }


    }
}
